﻿using Files_Upload.Domain;
using Files_Upload.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files_Upload.application
{
    public class ReportService : IReportService
    {
        private readonly IReportRepository _reportRepository;

        public ReportService(IReportRepository reportRepository)
        {
            _reportRepository = reportRepository;
        }

        public async Task<IEnumerable<Report>> GetAllReportsAsync(string appid, string date, string name, int state)
        {
            var reports = await _reportRepository.GetAllReportsAsync();
            if (!string.IsNullOrEmpty(appid))
            {
                reports = reports.Where(x => x.AppId == appid);
            }
            if (!string.IsNullOrEmpty(date))
            {
                reports = reports.Where(x => x.CreatedTime.ToString("yyyy-MM-dd").Contains(date));
            }
            if (!string.IsNullOrEmpty(name))
            {
                reports = reports.Where(x => x.FileName.Contains(name));
            }
            if (state != -1)
            {
                reports = reports.Where(x => x.ReportGenerated == state);
            }
            return reports;
        }

        public async Task<int> UploadFileAsync(string appId, string fileName, byte[] fileContent)
        {
            var report = new Report
            {
                AppId = appId,
                CreatedTime = DateTime.Now,
                FileName = fileName,
                ReportGenerated = 0
            };

            var sl = await _reportRepository.AddReportAsync(report);
            return sl;

        }

        public async Task<int> UpdateReportStatusAsync(string appId, string fileName)
        {
            var report = await _reportRepository.GetReportAsync(appId, fileName);
            if (report != null)
            {
                report.ReportGenerated = 1;
               var s= await _reportRepository.UpdateReportAsync(report);
                return s;

            }
           return 0;

        }
    }
}
